package com.jn.retrofit.presenter;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.jn.retrofit.common.WeakRrfHandler;
import com.jn.retrofit.contract.MusicContract;
import com.jn.retrofit.entity.MusicEntity;
import com.jn.retrofit.entity.Params;
import com.jn.retrofit.service.MusicService;
import com.jn.retrofit.utils.LogUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MusicPresenter implements MusicContract.IMusicPresenter {
    public static final int HTTP_SUCCESS = 0;
    public static final int HTTP_FAIL = 1;
    public static final int HTTP_SUCCESS2 = 2;

    private MusicContract.IMusicView mMainView;
    private Context mContext;


    public MusicPresenter(MusicContract.IMusicView mMainView, Context mContext) {
        this.mMainView = mMainView;
        this.mContext = mContext;
    }

    @Override
    public void getData(String keyWord) {//post
        LogUtils.d("asdf", "keyWord == " + keyWord);
        String url = "https://api.cenguigui.cn/api/";

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MusicService musicService = retrofit.create(MusicService.class);
        /*new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Response<ResponseBody> response = musicService.getData(keyWord).execute();
                    if (response.isSuccessful()) {
                        String body = response.body().string();
                        LogUtils.d("asdf", "onResponse : " + body);
                        Message message = Message.obtain();
                        message.obj = new String[]{body, url};
                        message.what = HTTP_SUCCESS;
                        mHandler.sendMessage(message);
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();*/

        /*new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Response<MusicEntity> response = musicService.getData2(keyWord).execute();
                    if (response.isSuccessful()) {
                        MusicEntity musicEntity = response.body();
                        Message message = Message.obtain();
                        message.obj = musicEntity;
                        message.what = HTTP_SUCCESS2;
                        mHandler.sendMessage(message);
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();*/

        /*new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Map<String, Object> map = new HashMap<>();
                    map.put("msg", keyWord);
                    Response<MusicEntity> response = musicService.getData3(map).execute();
                    if (response.isSuccessful()) {
                        MusicEntity musicEntity = response.body();
                        Message message = Message.obtain();
                        message.obj = musicEntity;
                        message.what = HTTP_SUCCESS2;
                        mHandler.sendMessage(message);
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();*/

        /*new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Map<String, Object> map = new HashMap<>();
                    map.put("msg", keyWord);
                    Response<MusicEntity> response = musicService.getData4(map).execute();
                    if (response.isSuccessful()) {
                        MusicEntity musicEntity = response.body();
                        Message message = Message.obtain();
                        message.obj = musicEntity;
                        message.what = HTTP_SUCCESS2;
                        mHandler.sendMessage(message);
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();*/

        /*new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Params params = new Params();
                    params.setMsg(keyWord);
                    Response<MusicEntity> response = musicService.getData5(params).execute();
                    if (response.isSuccessful()) {
                        MusicEntity musicEntity = response.body();
                        Message message = Message.obtain();
                        message.obj = musicEntity;
                        message.what = HTTP_SUCCESS2;
                        mHandler.sendMessage(message);
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();*/
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Response<MusicEntity> response = musicService.getData6(keyWord).execute();
                    if (response.isSuccessful()) {
                        MusicEntity musicEntity = response.body();
                        Message message = Message.obtain();
                        message.obj = musicEntity;
                        message.what = HTTP_SUCCESS2;
                        mHandler.sendMessage(message);
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
    }

    WeakRrfHandler<MusicPresenter> mHandler = new WeakRrfHandler<>(Looper.getMainLooper(), this, new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            int result = msg.what;
            switch (result) {
                case HTTP_SUCCESS:
                    String[] data = (String[]) msg.obj;
                    if (data == null || data.length < 2) {
                        return false;
                    }
                    String jsonStr = data[0];
                    Gson gson = new Gson();
                    try {
                        MusicEntity musicEntity = gson.fromJson(jsonStr, MusicEntity.class);
                        if (mMainView != null) {
                            mMainView.showData(musicEntity);
                        }
                    } catch (JsonSyntaxException e) {
                        LogUtils.d("asdf", "result not json");
                    }
                    break;
                case HTTP_SUCCESS2:
                    if (mMainView != null) {
                        mMainView.showData((MusicEntity) msg.obj);
                    }
                    break;
                case HTTP_FAIL:
                    break;
            }
            return false;
        }
    });
}